/****************************************************************
 * I am the AC 130 vs Pirate game. I allow the user to control an 
 * AC130 gunship in the hunt for Modern day pirates. The player  
 * will be allowed to roam free across a section of ocean/coastline.  
 * In the water there will be four types of vessels: Merchant  
 * ships that the player must protect, Pirate ships that the player  
 * must identify and destroy, Fishing boats that look like Pirate  
 * ships that the player must not destroy, and Friendly Navy ships  
 * that can capture pirates for extra points. Time permitting there  
 * may also be ground targets to attack. 
 *
 * Key elements of this project will be the Player/AC130, the Ships,  
 * the ShipAI system, the collision grid, the debris controller, the  
 * Ground/Water, Clouds, and scoring system. This project will make  
 * use of an external text file to store useful game parameters,  
 * thus allowing us to play balance the game without recompiling  
 * the executable. 
 *****************************************************************
 * I am the camera class. I contain all the information needed to
 * represent a camera in the world. The Camera's screen varialbe will 
 * act as the back buffer for the game. Its center point will represent
 * where in the world the center of the camera is looking at. Functions
 * like WtoS and StoW will convert world to screen coordinates and 
 * vise versa.
 *****************************************************************/

#include "camera.h"
#include <allegro.h>
#include <math.h>



/******************************************************************
 * I am the constructor. I create all data objects for this game.
 * I also handle default variable setting.
 ******************************************************************/
Camera::Camera(){

	center = new Point;
	out = new Point;

	screen = create_bitmap(SCREEN_W, SCREEN_H);
	worldSc = create_bitmap(MAXZOOM, MAXZOOM*6/8);
	xoff  = worldSc->w/2;
	yoff  = worldSc->h/2;
	
	zoom =  (float)(SCREEN_W)/(float)(worldSc->w);

}

Camera::~Camera(){
	delete(center);
	delete(out);
}


/******************************************************************
 * I am the initilization function. I save the external bitmap into
 * the internal varialbe src.
 ******************************************************************/
void Camera::Init(BITMAP *src){
	screen = src;
}


/******************************************************************
 * I am the inside screen checker funciotn I check weather or not
 * the point is inside the screen and there for if it should be rendered
 ******************************************************************/
bool Camera::IsInScreen(Point * tgt){
	if( tgt->x < center->x || tgt->x > center->x + SCREEN_W ||
		tgt->y < center->y || tgt->y > center->y + SCREEN_H )
	{
		return false;
	}
	return true;
}


/******************************************************************
 * I am the World to screen converter function. When I am done 
 * the out point will be in the screen location
 ******************************************************************/
Point * Camera::WTS(Point * tgt){
	out->x = WTSx(tgt->x);
	out->y = WTSy(tgt->y);

	return out;
}



/******************************************************************
 * I am the Screen to World converter function. When I am done 
 * the out point will be in the world location relative to the screen
 ******************************************************************/
Point * Camera::STW(Point * tgt){

	out->x = STWx(tgt->x);
	out->y = STWy(tgt->y);

	return out;
}


/******************************************************************
 * We are the individual emement converters. 
 ******************************************************************/
float Camera::STWx(float Xin){
	int srcWid = (float)(worldSc->w)*zoom;
	return (Xin-SCREEN_W/2)*srcWid/SCREEN_W+center->x;
}
float Camera::STWy(float Yin){
	int srcHgt = (float)(worldSc->h)*zoom;
	return (Yin-SCREEN_H/2)*srcHgt/SCREEN_W+center->y;
}
float Camera::WTSx(float Xin){
	return (Xin -center->x)+ xoff;
}
float Camera::WTSy(float Yin){
	return (Yin -center->y)+ yoff;
}

/******************************************************************
 * I am the pre overlay render funciton. I render the world to the 
 * scale specified by the zoom fraction. Further rendering can be done
 * as an overlay on screen
 ******************************************************************/
void Camera::RendWorld(){
	int srcWid = (float)(worldSc->w)*zoom;
	int srcHgt = (float)(worldSc->h)*zoom;

	stretch_blit(worldSc, this->screen, 
		xoff-srcWid/2, yoff-srcHgt/2, srcWid, srcHgt, 0, 0, 
		this->screen->w, this->screen->h);
}

/******************************************************************
 * I am the draw to world function I render the sprite into the world
 * bitmap, at the world X,Y coordinates.
 ******************************************************************/
void Camera::WorldDrawSprite(BITMAP * src, float x, float y){
	draw_sprite(worldSc, src, WTSx(x), WTSy(y));
}


void Camera::WorldPivotCtr(BITMAP * src, Point * ctr, float rotate){
	pivot_sprite(worldSc, src, WTSx(ctr->x), WTSy(ctr->y), src->w/2,  src->h/2, 
		ftofix(rotate*256/360));
}

